//
//  SHChangeSizeScrollView.m
//  hotel
//
//  Created by 周宇航 on 2017/3/2.
//  Copyright © 2017年 zj. All rights reserved.
//

#import "SHChangeSizeScrollView.h"




@implementation SHChangeSizeScrollView

-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    scrollframe = self.bounds;
    
//    csScrollWidth =frame.size.width;
//    csScrollHeight =frame.size.height;
//    self.csMultiple=201.0/235.0;
    return self;
}

-(void)setScrollViewWithArrData:(NSArray *)arrData{
    float scrollViewWidth = self.showBounds.size.width+self.intervalWight;
    
    if(!csScorllView){
        
        csScrollWidth = self.showBounds.size.width;
        csScrollHeight = self.showBounds.size.height;
        if (!self.isChangeSize) {
            self.csMultiple = 1;
        }
        
        csScorllView = [[UIScrollView alloc] initWithFrame:scrollframe];
        csScorllView.showsHorizontalScrollIndicator = NO;
        csScorllView.showsVerticalScrollIndicator = NO;
        csScorllView.pagingEnabled = YES;
        csScorllView.clipsToBounds = NO;
        [csScorllView setDelegate:self];
        imageViewArray=[NSMutableArray arrayWithCapacity:0];
    }
    [csScorllView removeAllSubviews];
   
    for(int i=0;i<arrData.count;i++)
    {
        CGRect imageRect;
        imageRect = CGRectMake( i*scrollViewWidth,(scrollframe.size.height-csScrollHeight)/2, csScrollWidth*self.csMultiple, csScrollHeight*self.csMultiple);
   
        UIView * roomView;
        if (i<arrData.count) {
            NSDictionary * roomInfo=arrData[i];
            roomView=[self loadRoomContentView:roomInfo];
        }else{
            roomView=[[UIView alloc]init];
        }
       
        roomView.frame=imageRect;
        [roomView setTag:100+i];
        [csScorllView addSubview:roomView];
        [imageViewArray addObject:roomView];
    }
    
    [csScorllView setContentSize:CGSizeMake(arrData.count*scrollViewWidth,scrollframe.size.height)];
    [self addSubview:csScorllView];
}

-(UIView *)loadRoomContentView:(NSDictionary *)roomInfo{

  
    UIView * roomView;
    if (self.className.length) {
        Class ownCollectionClass = NSClassFromString(self.viewClassName);
        roomView = [[ownCollectionClass alloc]initWithFrame:self.showBounds];
        [roomView performSelector:@selector(setOwnCollectionCellWithData:) withObject:roomInfo];
    }else{
        roomView=[[UIView alloc]initWithFrame:self.showBounds];
        roomView.backgroundColor = [UIColor orangeColor];
        
    }
    UITapGestureRecognizer * tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(actionTurnToRoomDetail:)];
    tapGesture.delegate = (id <UIGestureRecognizerDelegate>)self;
    [roomView addGestureRecognizer:tapGesture];
    return roomView;
    
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *view = [super hitTest:point withEvent:event];
    if ([view isEqual:self])
    {
        for (UIView *subview in csScorllView.subviews)
        {
            CGPoint offset = CGPointMake(point.x - csScorllView.frame.origin.x + csScorllView.contentOffset.x - subview.frame.origin.x,
                                         point.y - csScorllView.frame.origin.y + csScorllView.contentOffset.y - subview.frame.origin.y);

            if ((view = [subview hitTest:offset withEvent:event]))
            {
                return view;
            }
        }
        return csScorllView;
    }
    return view;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    endScrollSizeX =scrollView.contentOffset.x;
    NSLog(@"scrollView.contentOffset.x  =%f ",endScrollSizeX);
    
    if (!self.isChangeSize) {
        return;
    }
    
    float width = csScrollWidth-1;
    
    int indexNum=endScrollSizeX/width;
    for (int i=0;i<imageViewArray.count;i++) {
        if (i!=indexNum) {
            UIView *showV = [imageViewArray objectAtIndex:i];
            CGPoint center = showV.center;
            CGRect imageRect = CGRectMake( 0,0, csScrollWidth*self.csMultiple, csScrollHeight*self.csMultiple);
            [showV setBounds:imageRect];
            [showV setCenter:center];
        }else{
            UIView *showV = [imageViewArray objectAtIndex:i];
            CGPoint center = showV.center;
            CGRect imageRect = CGRectMake( 0,0, csScrollWidth*(self.csMultiple+0.1), csScrollHeight*(self.csMultiple+0.1));
            [showV setBounds:imageRect];
            [showV setCenter:center];
        }
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    
    float xOffset =scrollView.contentOffset.x;
    float width = csScrollWidth-1;
    int indexNum=xOffset/width;
    NSLog(@"第%d页 ",indexNum);
    
    if (!self.isChangeSize) {
        return;
    }
    
        float differences= endScrollSizeX - xOffset;
        float dif=fabsf(differences);
        if (dif>=(csScrollWidth/2)&&dif<=csScrollWidth) {
            int index=0;
            if (endScrollSizeX>xOffset) {
                index=endScrollSizeX/width-1;
            }else{
                index=endScrollSizeX/width+1;
            }
            if(index<[imageViewArray count]&&index>=0){
                UIView *showV = [imageViewArray objectAtIndex:index];
                //开始放大
                CGPoint center = showV.center;

                float proportion = (dif/(csScrollWidth/2)-1);
                CGRect imageRect = CGRectMake( 0,0, csScrollWidth*(self.csMultiple+0.1*proportion), csScrollHeight*(self.csMultiple+0.1*proportion));
                [showV setBounds:imageRect];
                [showV setCenter:center];
            }
        }

        
//        //第一页特殊
//        if([imageViewArray count] && indexNum==0)
//        {
//            UIView *showV = [imageViewArray objectAtIndex:indexNum];
//            CGPoint center = showV.center;
//            [UIView animateWithDuration:0.2 animations:^{
//                [showV setBounds:[self turnSmallSizeWithTag:showV.tag]];
//                [showV setCenter:center];
//            }];
//        }
    
    
    //向左滑
    if(endScrollSizeX > scrollView.contentOffset.x)
    {   int index=endScrollSizeX/width;
        if(index>=0 && index<[imageViewArray count]){
            float differences= endScrollSizeX - scrollView.contentOffset.x;
            if (differences<=(csScrollWidth/2)&&differences>=0) {
                UIView *showV = [imageViewArray objectAtIndex:index];
                CGPoint center = showV.center;
                float proportion = (1-differences/(csScrollWidth/2));
                CGRect imageRect = CGRectMake( 0,0, csScrollWidth*(self.csMultiple+0.1*proportion), csScrollHeight*(self.csMultiple+0.1*proportion));
                [showV setBounds:imageRect];
                [showV setCenter:center];
            }
        }
       //        if(indexNum+1>0 && indexNum+1<=[imageViewArray count])
//        {
//            UIView *showV = [imageViewArray objectAtIndex:indexNum+1];
//            CGPoint center = showV.center;
//            [UIView animateWithDuration:0.2 animations:^{
//                [showV setBounds:[self turnSmallSizeWithTag:showV.tag]];
//                [showV setCenter:center];
//            }];
//        }
    }
    
 
    //向右滑
    if(endScrollSizeX < scrollView.contentOffset.x)
    {
        int index=endScrollSizeX/width;
        if(index>=0 && index<[imageViewArray count]){
            float differences=  scrollView.contentOffset.x-endScrollSizeX;
            if (differences<=(csScrollWidth/2)&&differences>=0) {
                UIView *showV = [imageViewArray objectAtIndex:index];
                CGPoint center = showV.center;
                float proportion = (1-differences/(csScrollWidth/2));
                CGRect imageRect = CGRectMake( 0,0, csScrollWidth*(self.csMultiple+0.1*proportion), csScrollHeight*(self.csMultiple+0.1*proportion));
                [showV setBounds:imageRect];
                [showV setCenter:center];
            }
        }
        
//        csScrollWidth/2
//        if(indexNum-1>=0 && indexNum-1<[imageViewArray count])
//        {
//            UIView *showV = [imageViewArray objectAtIndex:indexNum-1];
//            CGPoint center = showV.center;
//            [UIView animateWithDuration:0.2 animations:^{
//                [showV setBounds:[self turnSmallSizeWithTag:showV.tag]];
//                [showV setCenter:center];
//            }];
//        }
    }
    
    endFloat = scrollView.contentOffset.x;
}

-(CGRect)turnSmallSizeWithTag:(NSInteger)tag{
    CGRect imageRect = CGRectMake( 0,0, csScrollWidth*self.csMultiple, csScrollHeight*self.csMultiple);
    return imageRect;
}

-(void)actionTurnToRoomDetail:(UITapGestureRecognizer*)gesture{
    if ([self.delegate respondsToSelector:@selector(csScrollView:detailView:)]) {
        [self.delegate csScrollView:self detailView:(int)gesture.view.tag-100];
    }
}

//-(void)actionBookHotelRoom:(UIButton *)sender{
//    if ([self.delegate respondsToSelector:@selector(csScrollView:orderBtn:)]) {
//        [self.delegate csScrollView:self orderBtn:(int)sender.superview.tag-100];
//    }
//}


@end
